package com.feicheng.iou.controller;


import com.alibaba.fastjson.JSONObject;
import com.feicheng.iou.bean.InformationBean;
import com.feicheng.iou.constant.SessionConstant;
import com.feicheng.iou.constant.StatusCodeConstant;
import com.feicheng.iou.exception.SystemExceptionV110;
import com.feicheng.iou.pojo.BaomingPOJO;
import com.feicheng.iou.pojo.login.LoginReturnPOJO;
import com.feicheng.iou.result.ResultInfoV110;
import com.feicheng.iou.service.BaomingService;
import com.feicheng.iou.util.IdempotentUtils;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
//import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author tianchunlei
 * @since 2021-06-28
 */
@RestController
@RequestMapping("/iou/baoming")
@Slf4j
@Setter
@CrossOrigin
public class BaomingController {


    @Autowired
    private BaomingService baomingService;

    /**
     * @param baomingStr，request
     * @return ResultInfoV110
     * @Author luoxin
     * @Description 添加报名信息的接口
     * @Date 11:17 上午 2021/8/12
     */
    @PostMapping
    public ResultInfoV110 addBaoming(String baomingStr, HttpServletRequest request) {   //添加报名信息
        log.info("addBaoming方法进入，参数为: " + baomingStr);

        //判断请求是否重复
        if (!IdempotentUtils.judge(baomingStr, this.getClass())) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F004, "报名添加"));
        }

        // 参数校验是否为空
        if (baomingStr == null) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F002, "报名"));
        }

        //字符串转换Json对象
        JSONObject jsonObject = JSONObject.parseObject(baomingStr);
        //json对象转换baomingpojo对象
        BaomingPOJO baomingPOJO = JSONObject.toJavaObject(jsonObject, BaomingPOJO.class);

        //调用baomingService实现线索添加业务逻辑
        return baomingService.insert(baomingPOJO, request);
    }

    /**
     * @param baomingStr
     * @return ResultInfoV110
     * @Author luoxin
     * @Description 查询报名信息列表的接口
     * @Date 11:17 上午 2021/8/12
     */
    @GetMapping("/list")
    public ResultInfoV110 selectBaomingList(String baomingStr, HttpServletRequest request) {  //查询报名信息列表
        log.info("selectResourceList方法进入，参数为: " + baomingStr);

        // 参数校验是否为空
        if (baomingStr == null) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F002, "查询报名"));
        }

        //字符串转换Json对象
        JSONObject jsonObject = JSONObject.parseObject(baomingStr);
        //json对象转换baomingpojo对象
        BaomingPOJO baomingPOJO = JSONObject.toJavaObject(jsonObject, BaomingPOJO.class);

        //调用baomingService实现线索列表查询业务逻辑
        return baomingService.selectBaominngList(baomingPOJO, request);
    }

    /**
     * @param id
     * @return ResultInfoV110
     * @Author luoxin
     * @Description 查询单个报名信息的接口
     * @Date 11:17 上午 2021/8/12
     */
    @GetMapping("/info/{id}")
    public ResultInfoV110 selectBaomingInfo(@PathVariable("id") Integer id) {  //查询单个报名信息
        log.info("selectBaomingInfo方法进入，参数为: " + id);

        // 参数校验是否为空
        if (id == null) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F002, "查询报名id"));
        }

        //调用baomingService实现线索单个查询业务逻辑
        return baomingService.selectBaominginfo(id);
    }


    /**
     * @param baomingStr
     * @return ResultInfoV110
     * @Author luoxin
     * @Description 修改报名信息
     * @Date 11:18 上午 2021/8/12
     */
    @PutMapping
    public ResultInfoV110 updateBaoming(String baomingStr) {  //修改报名信息
        log.info("updateBaoming方法进入，参数为: " + baomingStr);
        // 参数校验是否为空
        if (baomingStr == null) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F002, "修改报名"));
        }

        //字符串转换Json对象
        JSONObject jsonObject = JSONObject.parseObject(baomingStr);
        //json对象转换baomingpojo对象
        BaomingPOJO baomingPOJO = JSONObject.toJavaObject(jsonObject, BaomingPOJO.class);

        //调用baomingService实现线索修改业务逻辑
        return baomingService.update(baomingPOJO);

    }

    /**
     * @param baomingId,autorId
     * @return ResultInfoV110
     * @Author luoxin
     * @Description 分配报名线索的接口
     * @Date 11:40 上午 2021/8/12
     */
    @PutMapping("/autor")
    public ResultInfoV110 updateBaomingAutor(Integer baomingId, Integer autorId) {  //分配报名线索
        log.info("updateBaoming方法进入，参数为: " + baomingId, autorId);
        // 参数校验是否为空
        if (baomingId == null) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F002, "修改报名"));
        }
        // 参数校验是否为空
        if (autorId == null) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F002, "修改报名"));
        }

        //调用baomingService实现线索修改业务逻辑
        return baomingService.updateAutor(baomingId, autorId);

    }

    @GetMapping("/again/{studentId}")
    public ResultInfoV110 selectAgainShow(@PathVariable("studentId") Integer studentId) {  //查询报名信息列表
        log.info("selectResourceList方法进入，参数为: " + studentId);

        // 参数校验是否为空
        if (studentId == null) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F002, "学生ID"));
        }

        //调用baomingService实现线索列表查询业务逻辑
        return baomingService.selectAgainShow(studentId);
    }

    @PostMapping("/again")
    public ResultInfoV110 againBaoming(String baomingStr, HttpServletRequest request) {   //重新报名信息
        log.info("againBaoming方法进入，参数为: " + baomingStr);

        //判断请求是否重复
        if (!IdempotentUtils.judge(baomingStr, this.getClass())) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F004, "报名添加"));
        }

        // 参数校验是否为空
        if (baomingStr == null) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F002, "报名"));
        }

        //字符串转换Json对象
        JSONObject jsonObject = JSONObject.parseObject(baomingStr);
        //json对象转换baomingpojo对象
        BaomingPOJO baomingPOJO = JSONObject.toJavaObject(jsonObject, BaomingPOJO.class);

        //调用baomingService实现线索添加业务逻辑
        return baomingService.againInsert(baomingPOJO, request);
    }
}

